package com.medium.house.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
import com.medium.house.entity.WechatUser;
import com.medium.house.service.WechatUserService;
import java.util.HashMap;
import com.medium.house.repository.WechatUserMapper;
import org.springframework.stereotype.Service;

@Service
public class WechatUserServiceImpl implements WechatUserService
{

    @Autowired
    private WechatUserMapper wechatUserMapper;

    @Override
    public int add(WechatUser dto)
    {
        return wechatUserMapper.insert(dto);
    }

    @Override
    public int delete(long id)
    {
        return wechatUserMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int update(WechatUser dto)
    {
        return wechatUserMapper.updateByPrimaryKeySelective(dto);
    }

    @Override
    public WechatUser get(long id)
    {
        return wechatUserMapper.selectByPrimaryKey(id);
    }

    @Override
    public WechatUser getWechatUserByOpenid(String openid)
    {
        return wechatUserMapper.selectWechatUserByOpenid(openid);
    }

    @Override
    public WechatUser getWechatUserByOpenid(String openid,Integer status)
    {
        Map<String, Object> args = new HashMap<>();
        if (StringUtils.isEmpty(openid)){ args.put("openid",openid); }
        if (status > 0){ args.put("status",status); }
        return wechatUserMapper.selectWechatUserByArgs(args);
    }

    @Override
    public List<WechatUser> getByPage(
        int start,
        int limit,
        int orderby,
        int order)
    {
        if (start < 0) {start = 0;}
        if (limit <=0) {limit = 20;}
        if (orderby > WechatUserMapper.ORDERBY.length - 1 || orderby < 0) {orderby = 0;}
        if (order != 0) {order = 1;}

        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        return wechatUserMapper.selectByPage(start, limit, WechatUserMapper.ORDERBY[orderby], WechatUserMapper.ORDER[order], args);
    }

    @Override
    public List<Long> getIdsByPage(
        int start,
        int limit,
        int orderby,
        int order)
    {
        if (start < 0) {start = 0;}
        if (limit <=0) {limit = 20;}
        if (orderby > WechatUserMapper.ORDERBY.length - 1 || orderby < 0) {orderby = 0;}
        if (order != 0) {order = 1;}

        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        return wechatUserMapper.selectIdsByPage(start, limit, WechatUserMapper.ORDERBY[orderby], WechatUserMapper.ORDER[order], args);
    }

    @Override
    public long count()
    {
        Map<String, Object> args = new HashMap<>();
        // Add more parameters here
        return wechatUserMapper.count(args);
    }

}